/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.parts;

import framework.config.Utilities;
import framework.data.ReservaQuarto;
import framework.data.Pessoa;
import framework.data.Quarto;
import framework.db.PessoaManager;
import framework.db.QuartoManager;
import java.sql.Date;
import java.text.DateFormat;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Darlan
 */
public class ReservaQuartoTable extends JTable
{
    private static DateFormat df = DateFormat.getDateInstance();
    
    private List<ReservaQuarto> reservaQuartos;
    
    private enum Columns
    {
        ID( "Quarto" )
        {
            @Override
            public Object getValue( ReservaQuarto u )
            {
                String name = "Quarto não encontrado";
                
                try
                {
                    Quarto quarto = QuartoManager.getInstance().getQuarto( u.getCadastro() );
                    
                    if ( quarto != null )
                    {
                        name = String.valueOf( quarto.getNumero() );
                    }
                }
                
                catch ( Exception e )
                {
                    Utilities.handleException( e );
                }
                
                return name;
            }
        },
        CADASTRO( "Cadastro" )
        {
            @Override
            public Object getValue( ReservaQuarto u )
            {
                String name = "Quarto não encontrado";
                
                try
                {
                    Pessoa pessoa = PessoaManager.getInstance().getPessoa( u.getCadastro() );
                    
                    if ( pessoa != null )
                    {
                        name = String.valueOf( pessoa.getNome() );
                    }
                }
                
                catch ( Exception e )
                {
                    Utilities.handleException( e );
                }
                
                return name;
            }
        },
        DATA_ENTRADA( "Data entrada" )
        {
            @Override
            public Object getValue( ReservaQuarto u )
            {
                String date = "Sem data de entrada";
                Date d = u.getCheckIn();
                
                if ( d != null )
                {
                    date = df.format( d );
                }
                
                return date;
            }
        },
        DATA_SAIDA( "Data saída" )
        {
            @Override
            public Object getValue( ReservaQuarto u )
            {
                String date = "Sem data de entrada";
                Date d = u.getCheckIn();
                
                if ( d != null )
                {
                    date = df.format( d );
                }
                
                return date;
            }
        };
        
        private String name;
        
        private Columns( String name )
        {
            this.name = name;
        }
        
        /**
         * 
         * @return 
         */
        public String getName()
        {
            return name;
        }
        
        public abstract Object getValue( ReservaQuarto u );
    }

    /**
     * 
     */
    public ReservaQuartoTable()
    {
    }

    /**
     * 
     * @return 
     */
    public List<ReservaQuarto> getReservaQuartos()
    {
        return reservaQuartos;
    }

    /**
     * 
     * @param pessoas 
     */
    public void setReservaQuartos( List<ReservaQuarto> pessoas )
    {
        this.reservaQuartos = pessoas;
        
        refreshContent();
    }
    
    /**
     * 
     * @return 
     */
    public ReservaQuarto getSelectedReservaQuarto()
    {
        int selected = getSelectedRow();
        ReservaQuarto p = null;
        
        if ( selected >= 0 )
        {
            p = reservaQuartos.get( selected );
        }
        
        return p;
    }
    
    /**
     * 
     */
    public void refreshContent()
    {
        this.setModel( new UsuarioTableModel() );
    }
    
    /**
     * 
     */
    private class UsuarioTableModel extends DefaultTableModel
    {
        @Override
        public boolean isCellEditable( int row, int column )
        {
            return false;
        }

        @Override
        public int getColumnCount()
        {
            return Columns.values().length;
        }

        @Override
        public String getColumnName(int column)
        {
            return Columns.values()[column].getName();
        }
    
        @Override
        public Object getValueAt( int row, int column )
        {
            return Columns.values()[ column ].getValue( reservaQuartos.get( row ) );
        }

        @Override
        public int getRowCount()
        {
            return reservaQuartos.size();
        }

        @Override
        public Class<?> getColumnClass( int columnIndex )
        {
            return String.class;
        }
    }
}
